package com.rico.kok.tool;

import com.rico.kok.config.FileDirConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;

public class RegisterUtils {

    @Autowired
    FileDirConfig fileDirConfig;

    public void sendEmail(String userEmail, String checkCode, JavaMailSenderImpl mailSender, String propertiesFileName) {
        SimpleMailMessage message = new SimpleMailMessage();
        Properties properties = new Properties();
        try {
            properties.load(this.getClass().getClassLoader().getResourceAsStream(propertiesFileName));
        } catch (IOException e) {
            System.out.println("邮箱配置文件读取失败");
            e.printStackTrace();
            return;
        }
//        String filePath=new Object(){
//            public String getPath(){
//                return this.getClass().getResource(propertiesFileName).getPath();
//            }
//        }.getPath().substring(1);
//        System.out.println(filePath);
//        try {
//            InputStream inputStream=new FileInputStream(new File(filePath));
//            properties.load(inputStream);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        try {
            message.setFrom(String.valueOf(new InternetAddress(properties.getProperty("email.address"), properties.getProperty("email.personal"), "UTF-8")));
        } catch (UnsupportedEncodingException e) {
            System.out.println("不支持的编码格式");
            e.printStackTrace();
        }
        message.setTo(userEmail);
        message.setSubject(properties.getProperty("email.subject"));
        message.setText(properties.getProperty("email.content1") + checkCode + properties.getProperty("email.content2"));
        mailSender.send(message);
    }

    //文件上传方法
    public String updateFile(MultipartFile updatePic) {
        //-1：文件过大
        //-2：文件类型有误
        //1：文件上传成功

        //获取文件类型
        String type = updatePic.getOriginalFilename().substring(updatePic.getOriginalFilename().lastIndexOf(".") + 1);
        //实现分类
        //1， 获取当前日期
        Date date = new Date();
        // yyyy-MM-dd
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dirname = sdf.format(date);
        System.out.println(dirname);
        //String dirpath = session.getServletContext().getRealPath("WEB-INF/fileuploads") + "/" + dirname;
        String dirpath = fileDirConfig.getUserPicDir() + "/kok/src/userpic/update/" + dirname;
        //创建当前日期的文件夹
        System.out.println(dirpath);
        File dirfile = new File(dirpath);
        //判断 当前日期文件夹是否已存在
        if (!dirfile.exists()) {
            //创建当前目录
            dirfile.mkdirs();
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //实现上传
        File file = new File(dirfile + "/" + uuid + "." + type);
        try {
            updatePic.transferTo(file);
        } catch (IOException e) {
            System.out.println("文件上传失败");
            e.printStackTrace();
        }
        return "/kok/src/userpic/update/" + dirname + "/" + uuid + "." + type;
    }

    //确定验证码是否超时
    public boolean isOutTime(String attributeKey,HttpSession session) {
        if (session.getAttribute(attributeKey) != null) {
            Date sendTime = (Date) session.getAttribute(attributeKey);
            int res = RandomCreate.getMinute(sendTime, new Date());
            if (res < 10) {
                return true;
            }
        }
        return false;
    }

}
